<template>
  <view class="article-detail">
    <!-- 文章头部 -->
    <view class="article-header">
      <text class="title">{{ article.title }}</text>
      <view class="meta">
        <text class="category">{{ article.category }}</text>
        <text class="time">{{ article.publishTime }}</text>
      </view>
    </view>

    <!-- 文章内容 -->
    <view class="article-content">
      <rich-text :nodes="article.content"></rich-text>
    </view>

    <!-- 相关文章 -->
    <view class="related-articles" v-if="relatedArticles.length > 0">
      <view class="section-title">相关文章</view>
      <view class="article-list">
        <view 
          class="article-item" 
          v-for="(item, index) in relatedArticles" 
          :key="index"
          @tap="goToArticle(item.id)"
        >
          <image :src="item.image" mode="aspectFill" class="article-image"></image>
          <view class="article-info">
            <text class="article-title">{{ item.title }}</text>
            <text class="article-time">{{ item.publishTime }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      articleId: '',
      article: {
        title: '',
        category: '',
        publishTime: '',
        content: '',
        image: ''
      },
      relatedArticles: []
    }
  },

  onLoad(options) {
    if (options.id) {
      this.articleId = options.id
      this.loadArticleDetail()
    }
  },

  methods: {
    async loadArticleDetail() {
      try {
        // TODO: 替换为实际的API调用
        // const res = await api.getArticleDetail(this.articleId)
        this.article = {
          title: '春季养生指南',
          category: '养生保健',
          publishTime: '2024-03-06',
          content: '<div style="font-size: 28rpx; line-height: 1.6; color: #333;"><p>春季是养生的重要时节，本文将为您介绍春季养生的重要事项。</p><p>1. 起居有常：春季应该保持规律的作息，早睡早起。</p><p>2. 饮食调理：建议多食用温性食物，如韭菜、春笋等时令蔬菜。</p><p>3. 运动养生：可以选择慢跑、太极等适度运动。</p></div>',
          image: '/static/images/article1.jpg'
        }
        
        // 加载相关文章
        this.loadRelatedArticles()
      } catch (error) {
        console.error('加载文章详情失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },

    async loadRelatedArticles() {
      try {
        // TODO: 替换为实际的API调用
        // const res = await api.getRelatedArticles(this.articleId)
        this.relatedArticles = [
          {
            id: 2,
            title: '夏季养生要点',
            publishTime: '2024-03-05',
            image: '/static/images/article2.jpg'
          }
        ]
      } catch (error) {
        console.error('加载相关文章失败:', error)
      }
    },

    goToArticle(id) {
      uni.navigateTo({
        url: `/pages/community/article-detail?id=${id}`
      })
    }
  }
}
</script>

<style lang="scss">
.article-detail {
  min-height: 100vh;
  background: #fff;
  padding-bottom: 40rpx;

  .article-header {
    padding: 40rpx 30rpx;
    border-bottom: 1rpx solid #f5f5f5;
    
    .title {
      font-size: 40rpx;
      color: #333;
      font-weight: bold;
      line-height: 1.4;
      margin-bottom: 20rpx;
    }
    
    .meta {
      display: flex;
      align-items: center;
      
      .category {
        font-size: 24rpx;
        color: #007AFF;
        background: rgba(0,122,255,0.1);
        padding: 4rpx 12rpx;
        border-radius: 4rpx;
        margin-right: 20rpx;
      }
      
      .time {
        font-size: 24rpx;
        color: #999;
      }
    }
  }

  .article-content {
    padding: 30rpx;
  }

  .related-articles {
    margin-top: 40rpx;
    padding: 0 30rpx;
    
    .section-title {
      font-size: 32rpx;
      color: #333;
      font-weight: bold;
      margin-bottom: 20rpx;
    }
    
    .article-list {
      .article-item {
        display: flex;
        padding: 20rpx 0;
        border-bottom: 1rpx solid #f5f5f5;
        
        &:last-child {
          border-bottom: none;
        }
        
        .article-image {
          width: 200rpx;
          height: 140rpx;
          border-radius: 8rpx;
          margin-right: 20rpx;
        }
        
        .article-info {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          
          .article-title {
            font-size: 28rpx;
            color: #333;
            line-height: 1.4;
          }
          
          .article-time {
            font-size: 24rpx;
            color: #999;
          }
        }
      }
    }
  }
}
</style> 